
export default {
    tap (element, fn) {
        let startTx;
        let startTy;
        let endTx;
        let endTy;

        element.addEventListener('touchstart', (e) => {
            let touch = e.touches[0];
            startTx = touch.clientX;
            startTy = touch.clientY;
        }, false);

        element.addEventListener('touchend', (e) => {
            let touch = e.changedTouches[0];
            endTx = touch.clientX;
            endTy = touch.clientY;
            if(Math.abs(startTx - endTx) < 6 && Math.abs(startTy - endTy) < 6) {
                fn();
            }
        }, false);
    },

    swipeUp (element, fn) {
        let isTouchMove;
        let startTx;
        let startTy;

        element.addEventListener('touchstart', (e) => {
            let touch = e.touches[0];
            startTx = touch.clientX;
            startTy = touch.clientY;
            isTouchMove = false;
        }, false);

        element.addEventListener('touchmove', (e) => {
            isTouchMove = true;
            e.preventDefault();
        }, false);

        element.addEventListener('touchend', (e) => {
            if(!isTouchMove) {
                return;
            }

            let touch = e.changedTouches[0];
            let endTx = touch.clientX;
            let endTy = touch.clientY;
            let distanceX = startTx - endTx;
            let distanceY = startTy - endTy;
            let isSwipe = false;

            if(Math.abs(distanceX) < Math.abs(distanceY)) {
                if(distanceY > 20) {
                    fn();
                    isSwipe = true;
                }
            }
        }, false);
    }
}